﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Barry.BarbeSystem.Core;
using Barry.BarbeSystem.Models;
using NHibernate;
using NHibernate.Linq;
using System.Linq.Expressions;
namespace Barry.BarbeSystem.Business
{
    public class MemberRepository : Repository<Member>
    {
        public IList<Member> Get(MemberPageRequest request, out int itemCount)
        {
            request.cardId = request.cardId ?? "";
            request.name = request.name ?? "";
            ISession session = NHibernateHelper.OpenSession();
            var result = from item in session.Query<Member>()
                         where item.Enable == true
                         && item.CardId.Contains(request.cardId)
                         && item.Name.Contains(request.name)
                         orderby item.CardId descending
                         select item;
            itemCount = result.Count();
            return result.Skip((request.page - 1) * Utility.PageCount).Take(Utility.PageCount).ToList();
        }


        public IList<Member> GetAllMember()
        {
            ISession session = NHibernateHelper.OpenSession();

            var result = from item in session.Query<Member>()
                         where item.Enable == true
                         orderby item.CardId ascending
                         select item;
            return result.ToList();
        } 

        public IList<ConsumeHistory> GetHistory(MemberPageRequest request, out int itemCount)
        {
            request.cardId = request.cardId ?? "";
            request.name = request.name ?? "";
            ISession session = NHibernateHelper.OpenSession();
            var result = from item in session.Query<ConsumeHistory>()
                         where
                          item.Member.CardId.Contains(request.cardId)
                         && item.Member.Name.Contains(request.name)
                         orderby item.Id descending 
                         orderby item.Member.CardId descending
                         

                         select item;
            itemCount = result.Count();
            return result.Skip((request.page - 1) * Utility.PageCount).Take(Utility.PageCount).ToList();
        }

        public bool Recharge(Member member, ConsumeHistory history)
        {
            ISession session = NHibernateHelper.OpenSession();
            using (ITransaction trans = session.Transaction)
            {
                try
                {
                    trans.Begin();
                    if (!session.Contains(member))
                    {
                          session.Evict(member);
                    }
                  
                   session.Update(member);
                    history.Member = member;

                   //session.Merge(history);
                    session.Save(history);

                    session.Flush();
                    trans.Commit();
                    return true;
                }
                catch (Exception e)
                {
                    trans.Rollback();
                    throw e;
                }
            }
        }

        public bool Consume(Member member, ConsumeHistory history)
        {
            ISession session = NHibernateHelper.OpenSession();
            using (ITransaction trans = session.Transaction)
            {
                try
                {
                    trans.Begin();
                    if (!session.Contains(member))
                    {
                        session.Evict(member);
                    }
                    member.Balance = history.Balance;
                    session.Update(member);
                    history.Member = member;

                    //session.Merge(history);
                    session.Save(history);

                    session.Flush();
                    trans.Commit();
                    return true;
                }
                catch (Exception e)
                {
                    trans.Rollback();
                    throw e;
                }
            }
        }
    }
}